package com.ly.system.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.config.handler.BusinessException;
import com.ly.config.response.Result;
import com.ly.config.response.ResultCode;
import com.ly.system.entity.TbDepartment;
import com.ly.system.entity.TbUser;
import com.ly.system.service.TbDepartmentService;

import com.ly.system.vo.TbDepartmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.beans.binding.Bindings;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiangzhu
 * @since 2020-10-31
 */
@Api(value = "部门管理")
@RestController
@CrossOrigin
@RequestMapping("/Department")
public class TbDepartmentController {

    @Autowired
    private TbDepartmentService tbDepartmentService;

        @ApiOperation(value = "查询部门及人数",notes = "从部门表中分组查询用户人数")
    @PostMapping("/findDeptAndCount")
    public Result findDeptAndCount(){
        List<TbDepartmentVO> deptAndCount = tbDepartmentService.findDeptAndCount();


        if(deptAndCount.size()==0){
            throw  new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),
                    ResultCode.SUCCESS.getMessage());
        }
        return Result.ok().data("departments",deptAndCount);
    }
    @ApiOperation(value = "查询所有部门",notes = "从部门表中查询所有部门")
    @PostMapping("/findDept")
    public Result findDept( @RequestParam(required = true, defaultValue = "1") Integer pagenum,
                            @RequestParam(required = true, defaultValue = "3") Integer size,  @RequestParam(required = true, defaultValue ="") String name
                            ){
        Page iPage=new Page(pagenum,size);
        QueryWrapper<TbDepartment> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.eq("deleted",0);
        IPage<TbDepartment> deptAndCount22 = tbDepartmentService.findDeptAndCount22(iPage,queryWrapper);
        long total = deptAndCount22.getTotal();

        List<TbDepartment> records = deptAndCount22.getRecords();
        return Result.ok().data("total",total).data("TbDepartment",records);

    }

    @ApiOperation(value = "删除部门",notes = "从部门表中删除部门")
    @PostMapping("/delDept")
    public Result delDept(Integer id){
        boolean b = tbDepartmentService.removeById(id);

        return Result.ok().data("bool",b);
    }

    @ApiOperation(value = "修改部门",notes = "从部门表中修改部门")
    @PostMapping("/updDept")
    public Result updDept(@RequestBody TbDepartment tbDepartment){

        tbDepartment.setModifiedTime(new Date());
        tbDepartment.setCreateTime(new Date());
        boolean b = tbDepartmentService.updateById(tbDepartment);
        return Result.ok().data("bool",b);
    }
    @ApiOperation(value = "新增部门",notes = "从部门表中新增部门")
    @PostMapping("/addDept")
    public Result addDept(@RequestBody TbDepartment tbDepartment){

        tbDepartment.setModifiedTime(new Date());
        tbDepartment.setCreateTime(new Date());

        boolean b = tbDepartmentService.save(tbDepartment);
        return Result.ok().data("bool",b);
    }
    @PostMapping("/findDeptAndCount2")
    public Result findDeptAndCount2(){
        List<TbDepartmentVO> deptAndCount = tbDepartmentService.findDeptAndCount();
        List<String> dept=new ArrayList<>();
        List<String> count=new ArrayList<>();
        for (TbDepartmentVO tbDepartmentVO : deptAndCount) {
            dept.add(tbDepartmentVO.getName());
            count.add(tbDepartmentVO.getDeptCount().toString());
        }
        String[] dept2=dept.toArray(new String[dept.size()]);
        String[] count2=count.toArray(new String[count.size()]);

        if(deptAndCount.size()==0){
            throw  new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),
                    ResultCode.SUCCESS.getMessage());
        }

        return Result.ok().data("dept",dept2,"count",count2);
    }


}

